﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
{
    public  class LuceneTool
    {
        /// <summary>
        /// 定长截取没有HTML的字符串+ ... 
        /// </summary>
        /// <param name="value">字符串内容</param>
        /// <param name="len">长度</param>
        /// <returns></returns>
        public static string subHtmlString(object value, int len)
        {
            #region 定长截取没有HTML的字符串+ ...
            string pattern = @"<[^<>]+>";
            Regex rgx = new Regex(pattern);
            string outputStr = rgx.Replace(value.ToString(), "").Replace("&nbsp;", " ");
            if (outputStr.Length < len)
                return outputStr;//Encoding.Default.GetBytes(x).Length
            else
                return outputStr.Substring(0, len) + "...";
            #endregion
        }

        public static string ClearHtml(object value)
        {
            string pattern = @"<[^<>]+>";
            Regex rgx = new Regex(pattern);
            string outputStr = rgx.Replace(value.ToString(), "").Replace("&nbsp;", " ");
            return outputStr;
        }

        public static object DateFieldHander(object FieldValue)
        {
            string value = Convert.ToString(FieldValue);

            try
            {
                value = value.Substring(0, 8);
                if (value.Length == 8)
                {
                    StringBuilder builder = new StringBuilder();
                    builder.Append(value.Substring(0, 4));
                    builder.Append("-");
                    builder.Append(value.Substring(4, 2));
                    builder.Append("-");
                    builder.Append(value.Substring(6, 2));
                    return DateTime.Parse(builder.ToString());
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return DateTime.Now;

            }

        }

        /// <summary>
        /// 处理带高亮字符串的长度截取问题
        /// </summary>
        /// <param name="content"></param>
        /// <param name="length"></param>
        /// 高亮字符串:<span class="orange"></span>
        /// <returns></returns>
        public static string Substring(string content, int length)
        {

            StringBuilder builder = new StringBuilder(content);
            builder.Replace("\r", "");
            builder.Replace("\t", "");
            builder.Replace("\n", "");
            builder.Replace("#", "");
            builder.Replace("$", "");
            builder.Replace("<span class=\"orange\">", "#");
            builder.Replace("</span>", "$");

            Regex rex = new Regex(@"\$|#");
            int count1 = rex.Matches(builder.ToString()).Count;
            length = length + count1;

            string strHander = builder.ToString();

            #region
            //判断字符串长度是否大于截断长度
            if (System.Text.Encoding.Default.GetByteCount(strHander) > length)
            {
                //判断字串是否为空
                if (strHander == null)
                {
                    return "";
                }

                //初始化
                int i = 0, j = 0;

                //为汉字或全脚符号长度加2否则加1
                foreach (char newChar in strHander)
                {
                    if ((int)newChar > 127)
                    {
                        i += 2;
                    }
                    else
                    {
                        i++;
                    }
                    if (i > length)
                    {
                        strHander = strHander.Substring(0, j);
                        break;
                    }
                    j++;
                }
            }
            string result = string.Empty;
            if (!string.IsNullOrEmpty(strHander))
            {
                result = strHander.TrimEnd('#');
                int count = result.Split('#').Length - result.Split('$').Length;
                for (int i = 0; i < count; i++)
                {
                    result = result + "$";
                }
            }
            else
            {
                result = strHander;
            }
            if (!string.IsNullOrEmpty(result))
            {
                result = result.Replace("#", "<span class=\"orange\">");
                result = result.Replace("$", "</span>");
            }
            return result;
            #endregion
        }

    }
}
